package ma.fizazi.missionsmanagement.presentation.controllers;

import java.util.List;

import ma.fizazi.missionsmanagement.business.clients.ClientBusiness;
import ma.fizazi.missionsmanagement.business.clients.ContactBusiness;
import ma.fizazi.missionsmanagement.business.commons.CommonBusiness;
import ma.fizazi.missionsmanagement.business.utilisateurs.UtilisateurBusiness;
import ma.fizazi.missionsmanagement.model.Client;
import ma.fizazi.missionsmanagement.model.Contact;
import ma.fizazi.missionsmanagement.model.Utilisateur;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

@Controller
@RequestMapping("/clients")
public class ClientController {
	@Autowired
	UtilisateurBusiness utilisateurBusiness;
	@Autowired
	ClientBusiness clientBusiness;
	@Autowired
	ContactBusiness contactBusiness;
	@Autowired 
	CommonBusiness commonBusiness;
	
	
	@RequestMapping(method = RequestMethod.POST)
	public String processSubmit(@ModelAttribute("newClient") Client newClient,
			BindingResult result, SessionStatus status) {

		Client client = new Client();
		client.setId(newClient.getId());
		client.setAdresse(newClient.getAdresse());
		client.setNomClient(newClient.getNomClient());

		clientBusiness.saveClient(client);
		return "redirect:clients";
	}

	@RequestMapping(method = RequestMethod.GET)
	public String utilisateursPage(ModelMap model,
			@ModelAttribute("newClient") Client newClient) {

		// // return form view
		return "clients";
	}
	
	
	@RequestMapping(value="/clientContacts", method = RequestMethod.GET)
	public String contactsOfClientPage(@ModelAttribute("id") int clientId, @ModelAttribute("client") Client client,
			BindingResult result, SessionStatus status, ModelMap model) {
		
		client = clientBusiness.getClientById(clientId);
		List<Contact> contactsOfClient = clientBusiness.getActifsContactsOfClient(clientId);
		
//		for (Utilisateur utilisateur : utilisateursGroupe) {
//			System.out.println("Prenom : " + utilisateur.getPrenom());
//		}
		
		model.addAttribute("contactsOfClient", contactsOfClient);
		model.addAttribute("client", client);
		return "clientContacts";	
	}
	
	
	@RequestMapping(value="/clientContacts", method = RequestMethod.POST)
	public String addContactOfClientSubmit(@ModelAttribute("id") int clientId, @ModelAttribute("newContact") Contact newContact,
			BindingResult result, SessionStatus status, ModelMap model) {
		
		Client client = clientBusiness.getClientById(clientId);
		newContact.setClient(client);
		commonBusiness.saveEntity(newContact);
		
		return "redirect:clientContacts";	
	}
	

	@ModelAttribute("clientList")
	List<Client> getClientList() {
		return clientBusiness.getAllClients();
	}
	
	@ModelAttribute("newContact")
	Contact getNewContact()
	{
		Contact newContact = new Contact();
		return newContact;
	}
	
	
	@ModelAttribute("contactsOfClient")
	Contact getcontactsOfClient()
	{
		Contact newContact = new Contact();
		return newContact;
	}
	
	@ModelAttribute("utilisateurs")
	public List<Utilisateur> getUtilisateurs() {
		List<Utilisateur> utilisateurs = utilisateurBusiness
				.getAllConsultants();
		return utilisateurs;
	}
}